<template>
    <div>
        <!--      搜索  创建-->
        <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <el-input clearable v-model="form.theme" style="width: 300px" placeholder="活动主题"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="query">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="create">添加活动</el-button>
            </el-form-item>
        </el-form>
        <!--           列表-->
        <el-table :data="tableData" border>
            <el-table-column
                    fixed
                    prop="theme"
                    label="活动主题"
                    align="center">
            </el-table-column>
            <el-table-column
                    :formatter="formatDate"
                    prop="time"
                    label="活动时间"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="活动地点"
                    align="center">
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            type="success"
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            type="primary"
                            size="mini"
                            @click="signup(scope.row)">报名
                    </el-button>
                    <el-button
                            type="success"
                            size="mini"
                            @click="detail(scope.row)">详情
                    </el-button>
                    <el-button
                            type="danger"
                            size="mini"
                            @click="handleDelete(scope.row)" >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
<!--        分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalRecord" class="text-center mt10">
        </el-pagination>
<!--        创建弹出框-->
        <el-dialog title="创建" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.theme"></el-input>
                    </div>
                </el-form-item>

                <el-form-item label="活动时间" prop="time">
                    <div style="width: 320px">
                        <el-date-picker
                                style="width: 320px"
                                v-model="ruleForm.time"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleForm.address"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动流程" prop="flow">
                    <div style="width: 320px">
                        <el-input type="textarea" v-model="ruleForm.flow"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </div>
        </el-dialog>
<!--        报名弹出框-->
        <el-dialog title="报名" :visible.sync="signUpVisible">
            <el-form :model="signUpForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="姓名" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.name"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="年龄" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.age"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select style="width: 320px" v-model="signUpForm.sex" placeholder="请选择">
                        <el-option label="男" value="0">男</el-option>
                        <el-option label="女" value="1">女</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="家庭住址" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.address"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="民族" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.nation"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="联系电话" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.tel"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="身份证号" prop="applicant">
                    <div style="width: 320px">
                        <el-input v-model="signUpForm.idcard"></el-input>
                    </div>
                </el-form-item>
                <!--<el-form-item label="开始时间" prop="start_time">
                    <div style="width: 320px">
                        &lt;!&ndash; <el-input v-model="ruleFormEdit.time"></el-input> &ndash;&gt;
                        <el-date-picker
                                style="width: 320px"
                                v-model="signUpForm.start_time"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="结束时间" prop="end_time">
                    <div style="width: 320px">
                        &lt;!&ndash; <el-input v-model="ruleFormEdit.time"></el-input> &ndash;&gt;
                        <el-date-picker
                                style="width: 320px"
                                v-model="signUpForm.end_time"
                                type="date"
                                placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="signUpVisible = false">取 消</el-button>
                <el-button type="primary" @click="signupsure">确 定</el-button>
            </div>
        </el-dialog>
<!--        修改弹出框-->
        <el-dialog title="修改" :visible.sync="dialogFormVisibleEdit">
            <el-form :model="ruleFormEdit" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.theme"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动时间" prop="time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleFormEdit.time"></el-input> -->
                        <el-date-picker
                            style="width: 320px"
                            v-model="ruleFormEdit.time"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="ruleFormEdit.address"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动流程" prop="flow">
                    <div style="width: 320px">
                        <el-input type="textarea" v-model="ruleFormEdit.flow"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>
        </el-dialog>
<!--        详情弹出框-->
        <el-dialog title="修改" :visible.sync="detailVisible">
            <el-form :model="detailForm" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="活动主题" prop="theme">
                    <div style="width: 320px">
                        <el-input v-model="detailForm.theme"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="活动时间" prop="time">
                    <div style="width: 320px">
                        <!-- <el-input v-model="ruleFormEdit.time"></el-input> -->
                        <el-date-picker
                            style="width: 320px"
                            v-model="detailForm.time"
                            type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </el-form-item>
                <el-form-item label="活动地点" prop="address">
                    <div style="width: 320px">
                        <el-input v-model="detailForm.address"></el-input>
                    </div>
                </el-form-item>
                <!--<el-form-item label="报名人" prop="flow">
                    <div style="width: 320px">
                        <el-input v-model="detailForm.applicant"></el-input>
                    </div>
                </el-form-item>-->
                <el-form-item label="活动流程" prop="flow">
                    <div style="width: 320px">
                        <el-input type="textarea" v-model="detailForm.flow"></el-input>
                    </div>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>


    export default {

        name: 'test',

        data() {
            return {
                detailVisible:false,//详情弹出框
                detailForm:{},//详情表单
                signUpForm:{},//报名表单
                signUpVisible:false,//报名弹出框
                tableData: [],
                pageNo: 1,
                pageSize: 10,
                totalRecord: 0,
                totalPage: 0,
                dialogFormVisible: false, //创建弹出框
                dialogFormVisibleEdit:false, //修改弹出框
                form:{},  //创建按钮的表单
                //创建表单
                ruleForm: {
                    title: '',
                    content: '',
                },
                //修改表单
                ruleFormEdit:{
                    title: '',
                    content: '',
                },
                //验证
                rules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                //修改验证
                rulesEdit: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                formLabelWidth: '120px',
                cid:''
            }
        },

        props: {},

        components: {},

        computed: {},

        methods: {
            // 初始化
            async init() {
                const res = await this.$http('selectactlist',{
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                })
                // var timestamp = res.results.time;
                // console.log('birth',timestamp)
                // var d = new Date(timestamp);    //根据时间戳生成的时间对象
                // var date = (d.getFullYear()) + "-" +
                //     (d.getMonth() + 1) + "-" +
                //     (d.getDate())
                // res.results.time = date
                this.tableData = res.results

                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
                console.log('res',res)
            },
             formatDate(value){
                // <!--例如后台传给我们的时间戳  -->
                  this.value1= new Date(value.time);//value.createdTime是prop绑定的字段名称
                  let dateValue = this.$moment(this.value1).format("YYYY-MM-DD");//$moment专门转化时间的插件（使用时需要下载引入）
                  return dateValue
            },

            handleClick(row) {
                console.log(row);
            },
            //分页
            async handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            async handleCurrentChange(val) {
                this.pageNo = val;
                this.init();
            },
            //创建
            create() {
                this.dialogFormVisible = true
                this.ruleForm.title = ""
                this.ruleForm.content = ""
            },
            //点击确定立即创建
            async sure(){
                var timestamp = this.ruleForm.time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.ruleForm.time = date
                const res = await this.$http('createactivity',{
                    theme:this.ruleForm.theme,
                    time:this.ruleForm.time,
                    address:this.ruleForm.address,
                    flow:this.ruleForm.flow,
                })
                if (res.count === 1) {
                    this.$message({
                        message: '创建成功',
                        type: 'success'
                    });
                    this.dialogFormVisible = false
                    this.init()
                } else {
                    this.$message({
                        message: '创建失败',
                        type: 'error'
                    });
                }
            },
            //点击报名
            async signup(row){
                this.signId = row.id
                this.signUpVisible = true
            },
            //报名确定
            async signupsure(){
                const res = await this.$http('createenlist',{
                    theme_id:this.signId,
                    name:this.signUpForm.name,
                    age:this.signUpForm.age,
                    sex:this.signUpForm.sex,
                    address:this.signUpForm.address,
                    nation:this.signUpForm.nation,
                    tel:this.signUpForm.tel,
                    idcard:this.signUpForm.idcard,
                })
                if (res.count === 1) {
                    this.$message({
                        message: '报名成功',
                        type: 'success'
                    });
                    this.signUpVisible = false
                    this.init()
                    this.signUpForm = {}
                } else {
                    this.$message({
                        message: '报名失败',
                        type: 'error'
                    });
                }
            },
            //详情
            async detail(row){
                this.detailVisible = true
                this.detailForm = row
            },
            //编辑
            handleEdit(row){
                this.dialogFormVisibleEdit = true
                this.cid = row.id
                this.ruleFormEdit = row
            },
            //点击确定即可完成编辑
            async yes(){
                var timestamp = this.ruleFormEdit.time;
                console.log('birth',timestamp)
                var d = new Date(timestamp);    //根据时间戳生成的时间对象
                var date = (d.getFullYear()) + "-" +
                    (d.getMonth() + 1) + "-" +
                    (d.getDate())
                this.ruleFormEdit.time = date
                const res = await this.$http('createactivity',{
                    id:this.cid,
                    theme:this.ruleFormEdit.theme,
                    time:this.ruleFormEdit.time,
                    address:this.ruleFormEdit.address,
                    flow:this.ruleFormEdit.flow,
                })
                if (res.count === 1) {
                    this.$message({
                        message: '编辑成功',
                        type: 'success'
                    });
                    this.dialogFormVisibleEdit = false
                    this.init()
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error'
                    });
                }
            },
            //删除
            async handleDelete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('createactivity',{
                        id:row.id,
                        del_flag:'1'
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //搜索
            async query(){
                const res = await this.$http('selectactlist',{
                    theme:this.form.theme
                })
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            }
        },

        watch: {},

        mounted() {
            this.init();
        },

    }
</script>

<style scoped>

</style>
